import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class TestExport {
    public static void main(String[] args) {
        try {
            String exportDir = "./exports";
            // 确保目录存在
            File directory = new File(exportDir);
            if (!directory.exists()) {
                boolean created = directory.mkdirs();
                System.out.println("目录创建: " + created);
            }
            
            // 创建Excel文件
            String filePath = exportDir + "/test_" + System.currentTimeMillis() + ".xlsx";
            System.out.println("开始创建Excel文件: " + filePath);
            
            Workbook workbook = new XSSFWorkbook();
            Sheet sheet = workbook.createSheet("测试数据");
            
            // 创建表头
            Row headerRow = sheet.createRow(0);
            headerRow.createCell(0).setCellValue("ID");
            headerRow.createCell(1).setCellValue("名称");
            headerRow.createCell(2).setCellValue("时间");
            
            // 添加数据
            for (int i = 1; i <= 5; i++) {
                Row row = sheet.createRow(i);
                row.createCell(0).setCellValue(i);
                row.createCell(1).setCellValue("测试数据 " + i);
                row.createCell(2).setCellValue(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
            }
            
            // 自动调整列宽
            for (int i = 0; i < 3; i++) {
                sheet.autoSizeColumn(i);
            }
            
            // 写入文件
            File file = new File(filePath);
            try (FileOutputStream fileOut = new FileOutputStream(file)) {
                workbook.write(fileOut);
                fileOut.flush();
                System.out.println("Excel文件写入内容完成，路径: " + file.getAbsolutePath());
            }
            workbook.close();
            
            // 检查文件是否创建成功
            File checkFile = new File(filePath);
            if (checkFile.exists()) {
                System.out.println("Excel文件创建成功，大小: " + checkFile.length() + " 字节");
                
                // 读取文件内容
                byte[] content = Files.readAllBytes(Paths.get(filePath));
                System.out.println("读取文件内容成功，大小: " + content.length + " 字节");
            } else {
                System.err.println("文件创建失败，文件不存在: " + filePath);
            }
        } catch (IOException e) {
            System.err.println("创建测试文件失败: " + e.getMessage());
            e.printStackTrace();
        }
    }
} 